﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace GraphicsEngineNamespace
{
    /// <summary>
    /// Klasa serializująca inne kalsy.
    /// </summary>
    public class Serialization
    {
        /// <summary>
        /// Serializacja.
        /// </summary>
        public static void Serialize(Object o, string path)
        {
            XmlSerializer s = new XmlSerializer(o.GetType());
            TextWriter w = new StreamWriter(path + ".xml");
            s.Serialize(w, o);
            w.Close();
        }

        /// <summary>
        /// Deserializacja. Jak niema pliku zwraca zwraca null.
        /// </summary>
        public static Object Deserialize(Object o, string path)
        {
            XmlSerializer s = new XmlSerializer(o.GetType());
            TextReader r;
            try
            {
                r = new StreamReader(path + ".xml");
                o = s.Deserialize(r);
                r.Close();
                return o;
            }
            catch (Exception)
            {
                return null;
            }       
        }
    }
}